package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.utils.Constants;
import com.mzxy.utils.SpUtiles;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView et_psw_again;
    private TextView et_user_name;
    private TextView et_psw;
    private Button btn_register;
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout rl_title_bar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        initView();
        initEvent();

    }

    private void initEvent() {
        btn_register.setOnClickListener(this);
        tv_back.setOnClickListener(this);
    }

    private void initView() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("注册");
        tv_back = (TextView) findViewById(R.id.tv_back);
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        btn_register = (Button) findViewById(R.id.btn_register);
        et_user_name = (TextView) findViewById(R.id.et_user_name);
        et_psw = (TextView) findViewById(R.id.et_psw);
        et_psw_again = (TextView) findViewById(R.id.et_psw_again);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_register:
                register();
                break;
            case R.id.tv_back:
                finish();
                break;

        }
    }

    private void register() {
        SharedPreferences sp = getSharedPreferences(Constants.SP_NAME,MODE_PRIVATE);
        String username = et_user_name.getText().toString();
        String password = et_psw.getText().toString();
        String repassword = et_psw_again.getText().toString();
        //判断是否有输入
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)|| TextUtils.isEmpty(repassword)){
            Toast.makeText(this,"账号和密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (sp.getString(username,null)!=null){
            Toast.makeText(this,"用户名"+username+"已存在",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!password.equals(repassword)){
            Toast.makeText(this,"两次密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }
        //第三步，保存用户名和密码到SharePreference
        SpUtiles.saveRegisterInfo(this,username,password);
        Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();

        Intent date = new Intent();
        date.putExtra("username",username);
        date.putExtra("password",password);
        setResult(0x002,date);
        finish();
    }


    // private void init() {
       // tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        //tv_main_title.setText("注册");
        //tv_back = (TextView) findViewById(R.id.tv_back);
        //rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        //rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        //btn_register =
    //}

}
